package lumis.service.seo.robotstxt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalContext;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.propertybag.PropertyBag;
import lumis.portal.url.IURLManager;
import lumis.util.ITransaction;

/**
 * Populates the source with the served urls defined in the PropertyBag.
 * If there's not an url, the Portal's urls is used to populate the source.
 * 
 *
 * @version $Revision: 10825 $ $Date: 2009-08-04 12:36:40 -0300 (Tue, 04 Aug 2009) $
 * @since 5.0.0
 */
public class RobotsTxtServedUrlsDataProvider  implements IDataProvider<TabularSource<?>>
{
	public void loadData(SessionConfig sessionConfig, TabularSource<?> source, ITransaction transaction) throws lumis.portal.PortalException 
	{
		String channelId = source.getDouiContext().getRequest().getServiceInstanceConfig().getChannelId();
		ChannelConfig channelConfig = ManagerFactory.getChannelManager().get(sessionConfig, channelId, transaction);
		String propertyBagId = channelConfig.getPropertyBagId();
		PropertyBag propertyBag = ManagerFactory.getPropertyBagManager().get(propertyBagId);
		List<String> servedUrlValues = new ArrayList<String>();
		servedUrlValues.addAll(propertyBag.getValues(IURLManager.WEBSITE_PROPERTY_NAME, Collections.<String>emptyList()));
		servedUrlValues.addAll(propertyBag.getValues(
				"lumis.service.seo.robotstxt.url", Collections.<String>emptyList()));
		if(servedUrlValues.isEmpty())
			servedUrlValues.add(PortalContext.getFrameworkUrl());
		TabularData data = source.getData();
		for(String servedUrl: servedUrlValues)
		{
			// TODO: Verifies if the url is valid.
			ISourceData newRow = data.addRow();
			newRow.put(source.getPrimaryNameField().getId(), servedUrl);
		}
	}
}
